/**
 * 
 */
package info.niwota.profile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.net.Uri;
import android.provider.MediaStore.Images.ImageColumns;
import android.widget.Toast;

/**
 * @author qiangli
 * 
 */
public class Utils {

	/**
	 * 
	 */
	private Utils() {

	}

	public static void startViewActivity(Context ctx, Uri uri, String mimetype) {
		try {
			Intent i = new Intent(Intent.ACTION_VIEW);
			i.setDataAndType(uri, mimetype);
			//
			final String name = uri.getLastPathSegment();
			if (ctx.getPackageManager().resolveActivity(i,
					PackageManager.MATCH_DEFAULT_ONLY) != null) {
				ctx.startActivity(Intent.createChooser(i, name));
				return;
			}
			ctx.startActivity(Intent.createChooser(i, name));
		} catch (ActivityNotFoundException e) {
			Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_SHORT);
		} catch (Exception e) {
			Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_SHORT);
		}
	}

	public static String getDataPath(Context ctx, Uri uri) {
		String p = null;
		try {
			Cursor c = ctx.getContentResolver().query(uri,
					new String[] { ImageColumns.DATA }, null, null, null);
			if (c.moveToFirst()) {
				p = c.getString(0);
				return p;
			}
			c.close();
		} catch (Exception e) {
			if (Debug.DEBUG) {
				e.printStackTrace();
			}
		}
		return p;
	}

	public static File transformImage(File image, File newImage, int width,
			int height) throws Exception {

		try {
			transform(image, newImage, width, height);
		} catch (Exception e) {
			e.printStackTrace();

			newImage.delete();
			//
			throw e;
		}

		return newImage;
	}

	private static void transform(File image, File thumb, int width, int height)
			throws IOException {
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new FileInputStream(image);
			out = new FileOutputStream(thumb);
			thumbnail(in, out, width, height);

		} finally {
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.close();
			}
		}
	}

	public static void thumbnail(InputStream in, OutputStream out, int width,
			int height) {
		Bitmap image = BitmapFactory.decodeStream(in);
		int w = image.getWidth();
		int h = image.getHeight();
		float scalew = ((float) width) / w;
		float scaleh = ((float) height) / h;
		float scale = Math.min(scalew, scaleh);
		Matrix matrix = new Matrix();
		matrix.postScale(scale, scale);
		Bitmap resized = Bitmap.createBitmap(image, 0, 0, w, h, matrix, true);
		resized.compress(Bitmap.CompressFormat.JPEG, 50, out);
	}
	
	public static Bitmap thumb(Bitmap image, int width, int height) {
		int w = image.getWidth();
		int h = image.getHeight();
		float scalew = ((float) width) / w;
		float scaleh = ((float) height) / h;
		float scale = Math.min(scalew, scaleh);
		Matrix matrix = new Matrix();
		matrix.postScale(scale, scale);
		Bitmap thumb = Bitmap.createBitmap(image, 0, 0, w, h, matrix, true);
		return thumb;
	}

	public static Bitmap decorate(Bitmap image, int width, int height, Bitmap deco) {
		Bitmap thumb = thumb(image, width, height);
		final Canvas canvas = new Canvas(thumb);
		canvas.setDrawFilter(new PaintFlagsDrawFilter(
				Paint.DITHER_FLAG, 0));
		if (deco != null) {
			canvas.drawBitmap(deco, new Matrix(), null);
		}
		return thumb;
	}
}
